﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class ListNewsByGroupItems : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "ListNewsByGroupItems";
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string ActiveLanguage,string ListNewsByGroupId)
        {
            try
            {
                DataTable dtListNewsByGroupItem = new ListNewsByGroupBLL().ReadingListNewsByGroupItems(ListNewsByGroupId);
                string RetHtml =
                                "<div id=divProcessing></div>";
                if (dtListNewsByGroupItem.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                                "<table>\r\n" +
                                "   <tr>\r\n" +
                                "       <th>" + ResourceBLL.GetText(WebPartId, "GroupName", "Tên nhóm") + "</th>\r\n" +
                                "       <th>" + ResourceBLL.GetText(WebPartId, "IndexOrder", "Vị trí") + "</th>\r\n" +
                                "       <th>" + ResourceBLL.GetText(WebPartId, "MaxNews", "Số tin") + " </th>\r\n" +
                                "       <th></th>\r\n" +
                                "       <th>" + ResourceBLL.GetText(WebPartId, "Delete", "Xóa") + "</th>\r\n" +
                                "   </tr>";

                    for (int iIndex = 0; iIndex < dtListNewsByGroupItem.Rows.Count; iIndex++)
                    {
                        string ListNewsByGroupItemId = (string)dtListNewsByGroupItem.Rows[iIndex]["ListNewsByGroupItemId"];
                        string Subject = "";
                        try
                        {
                            Subject = (string)dtListNewsByGroupItem.Rows[iIndex][ActiveLanguage + "Subject"];
                        }
                        catch { }
                        int IndexOrder = (int)dtListNewsByGroupItem.Rows[iIndex]["IndexOrder"];
                        int MaxNews = (int)dtListNewsByGroupItem.Rows[iIndex]["MaxNews"];

                        string DeleteUrl = "javascript:DeleteListNewsByGroupItem('" + ListNewsByGroupId + "','" + ListNewsByGroupItemId + "');";
                        RetHtml += "<tr>\r\n" +
                                   "    <td class=cssCol1><input type=hidden id=txtId_" + iIndex + " value=\"" + ListNewsByGroupItemId + "\"><input id=txtSubject_" + iIndex + " class=cssSubjectItem value=\"" + Subject + "\"></td>\r\n" +
                                   "    <td class=cssCol2><input id=txtIndexOrder_" + iIndex + " class=cssIndexOrder value=\"" + IndexOrder + "\"></td>\r\n" +
                                   "    <td class=cssCol3><input id=txtMaxItem_" + iIndex + " class=cssMaxItems value=\"" + MaxNews + "\"></td>\r\n" +
                                   "    <td class=cssCol5><a href=\"javascript:PublishingListNewsByGroupItem('" + ListNewsByGroupItemId + "');\">" + ResourceBLL.GetText(WebPartId, "PublishingItemText", "(xuất bản)") + "</a></td>\r\n" +
                                   "    <td class=cssCol4><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "Delete", "Xóa") + "</a></td>\r\n" +
                                   "</tr>\r\n";
                    }
                    RetHtml += "</table>\r\n";
                }
               
                RetHtml += "<input type=hidden id=txtMaxGroupNewsRecordCount value=\"" + dtListNewsByGroupItem.Rows.Count + "\">";
                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            string ListNewsByGroupId = (string)System.Web.HttpContext.Current.Request["ListNewsByGroupId"];

            string SelectLanguageText =
                "<select id=divSelectLanguage onchange=javascript:DetectHtmlBody('" + ListNewsByGroupId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            AjaxPro.Utility.RegisterTypeForAjax(typeof(ListNewsByGroupItems));
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "ListNewsByGroup.js");

            string ListNewsByGroupItemAddRootUrl = "javascript:ClientShowAddListNewsByGroupItemRootForm('" + ListNewsByGroupId + "');";
            RetHtml +=

                //"<div class=cssForm>" +
                ///"<div class=cssFormTitle>" + ResourceBLL.GetText(WebPartId,"ListNewsByGroupItemHeader","Tin bài theo nhóm") + "</div>"+
                Environment.Environments.GetFormDialog(
                "<div class=cssListNewsByGroupItemAddRoot>" + SelectLanguageText + "<input type=button onclick=\""+ ListNewsByGroupItemAddRootUrl +"\" value=\"" + ResourceBLL.GetText(WebPartId, "ListNewsByGroupItemAddRoot", "Thêm") + "\">&nbsp;<input onclick=\"javascript:ClientSaveListNewsByGroupItems('" + ListNewsByGroupId + "');\" type=button value=\"" + ResourceBLL.GetText(WebPartId, "Save", "Ghi lại") + "\">&nbsp;<input type=button onclick=\"javascript:ClientRefreshList('"+ListNewsByGroupId+"');\" value=\"" + ResourceBLL.GetText(WebPartId, "RefreshButton", "Đọc lại") + "\">&nbsp;<input type=button onclick=\"javascript:window.close();\" value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Đóng") + "\"></div>" +
                "<div id=divContentData>" + GetHtml(Environments.ActiveLanguage, ListNewsByGroupId) + "</div>", ResourceBLL.GetText(WebPartId, "ListNewsByGroupItemHeader", "Tin bài theo nhóm"));
            

            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddListNewsByGroupItemFormUrl(string ListNewsByGroupId)
        {
            return Screen.BuildDialogUrl(new ParamCls[]{
                new ParamCls("WebPartId",new AddListNewsByGroupItem().WebPartId),
                new ParamCls("ListNewsByGroupId",ListNewsByGroupId)
            });
        }

        [AjaxPro.AjaxMethod]
        public string GetAddSubListNewsByGroupItemFormUrl(string ListNewsByGroupId, string ParentListNewsByGroupItemId)
        {
            return Screen.BuildDialogUrl(new ParamCls[]{
                new ParamCls("WebPartId",new AddListNewsByGroupItem().WebPartId),
                new ParamCls("ListNewsByGroupId",ListNewsByGroupId),
                new ParamCls("ParentListNewsByGroupItemId",ParentListNewsByGroupItemId),
            });
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteListNewsByGroupItem(string ListNewsByGroupItemId)
        {
            try
            {
                new ListNewsByGroupBLL().DeleteListNewsByGroupItem(null, ListNewsByGroupItemId);
                return ResourceBLL.GetText(WebPartId,"DeleteOkMessage","Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideSaveListNewsByGroupItem(string ActiveLanguage,string ListNewsByGroupItemId,string Subject,int IndexOrder,int MaxNews)
        {
            try
            {
                new ListNewsByGroupBLL().
                    SaveListNewsByGroupItem(null,ActiveLanguage, ListNewsByGroupItemId,Subject,IndexOrder,MaxNews);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideGetSaveOkMsg()
        {
            return ResourceBLL.GetText(WebPartId,"MessageUpdateOk","Cập nhật thành công!");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmPublishingMsg", "Bạn đã chắc chắn phát hành lên website public chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideItemPublishing(string ListNewsByGroupItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                new ListNewsByGroupBLL().ItemPublishingPublicWebSite(ListNewsByGroupItemId);
                return ResourceBLL.GetText(WebPartId, "PublishingOk", "Xuất bản lên website thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            return ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang xử lý dữ liệu ...");
        }

    }
}
